﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace Ares.Engine
{
    public class BackupProtocol
    {
        public uint id;
        public DateTime Timestamp { get; set;}
        public List<BackupEntry> BackupEntries = new List<BackupEntry>();
        public BackupMethod UsedMethod;
        public Guid ProjectGuid;
        public String Filename;

        public override string ToString()
        {
            return Timestamp.ToString();
        }

        public static BackupProtocol LoadBackupProject(string ProjectFile)
        {
            BackupProtocol tmpBP = new BackupProtocol();
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProtocol));
            TextReader tr = new StreamReader(ProjectFile);
            tmpBP = (BackupProtocol)serializer.Deserialize(tr);
            tr.Close();

            return tmpBP;
        }

        public static BackupProtocol LoadBackupProjectFromStream(MemoryStream Stream)
        {
            BackupProtocol tmpBP = new BackupProtocol();
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProtocol));
            Stream.Seek(0, SeekOrigin.Begin);
            tmpBP = (BackupProtocol)serializer.Deserialize(Stream);
            return tmpBP;
        }

        public static BackupProtocol LoadBackupProjectFromXml(String Xml)
        {
            BackupProtocol tmpBP = new BackupProtocol();
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProtocol));
            StringReader str = new StringReader(Xml);
            tmpBP = (BackupProtocol)serializer.Deserialize(str);
            return tmpBP;
        }

        public static void SaveBackupProject(string ProjectFile, BackupProtocol BP)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProtocol));
            TextWriter tw = new StreamWriter(ProjectFile);
            serializer.Serialize(tw, BP);
            tw.Close();
        }

        public static String SaveBackupProjectToXML(BackupProtocol BP)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(BackupProtocol));
            MemoryStream MS = new MemoryStream();
            serializer.Serialize(MS, BP);
            return Encoding.Default.GetString(MS.ToArray());
        }
    }
}